﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace SIN.Client.Model
{
    class webClientModel
    {
        public void downloadList()
        {
            try
            {
                //                Random aRandom = new Random();
                string aPath = Program.addonsListModel.dateIdentifierPath();
                Program.mainForm.setStatus("Addons list downloading started");
                Program.mainForm.setIdle(true);
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadFileCompleted);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
                webClient.DownloadFileAsync(new Uri("http://ui9.ru"), @aPath);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                //      Program.mainForm.setStatus("Could not download file: " + ex.Message);
                Console.WriteLine("Could not download file: " + ex.Message);
            }
        }

        public void downloadInfo(string aAddonName)
        {
            try
            {
                string aPath = Program.addonInfoModel.addonIdentifierPath(aAddonName);
                Program.mainForm.setStatus("Get information for \"" + aAddonName.Remove(aAddonName.Length - 1) + "\". Downloading started");
                Program.mainForm.setIdle(true);
                WebClient webClient = new WebClient();
                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(downloadInfoCompleted);
                webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloadProgressChanged);
                webClient.DownloadFileAsync(new Uri("http://ui9.ru/forum/" + aAddonName.Remove(aAddonName.Length - 1).ToLower() + "/about"), @aPath);
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                //      Program.mainForm.setStatus("Could not download file: " + ex.Message);
                Console.WriteLine("Could not download file: " + ex.Message);
            }
        }

        private void downloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Program.mainForm.setStatus(String.Format("{0} of {1} bytes downloaded ({2}% done)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage));
            Console.WriteLine(String.Format("{0} of {1} bytes downloaded ({2}% done)", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage));
        }

        private void downloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Program.mainForm.setStatus("All done downloading the file");
                Program.mainForm.setIdle(false);
                Program.mainForm.listDownloaded();
                Console.WriteLine("All done downloading the file");
            }
            else
            {
                Program.mainForm.setStatus("Could not download file: " + e.Error.Message);
                Console.WriteLine("Could not download file: " + e.Error.Message);
            }
        }

        private void downloadInfoCompleted(object sender, AsyncCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Program.mainForm.setStatus("Addon information updated");
                Program.mainForm.setIdle(false);
                Program.mainForm.infoDownloaded();
                Console.WriteLine("Addon information updated");
            }
            else
            {
                Program.mainForm.setStatus("Could not download file: " + e.Error.Message);
                Console.WriteLine("Could not download file: " + e.Error.Message);
            }

        }

    }

}
